package com.kt.uokos.yimo.wx.controller;

import com.kt.uokos.yimo.dao.Category;
import com.kt.uokos.yimo.dao.Product;
import com.kt.uokos.yimo.service.*;
import com.kt.uokos.yimo.util.ResponseUtil;
import com.kt.uokos.yimo.wx.annocation.LoginUser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@RestController
@RequestMapping("/wx/home")
@Validated
public class WxHomeController {


    private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(9);

    private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();

    private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(9, 9, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
    @Resource
    private AdService adService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private CouponService couponService;

    @Resource
    private ProductService productService;

    @Resource
    private BrandService brandService;

    @Resource
    private TopicService topicService;

    @GetMapping("/index")
    public Object index(@LoginUser Integer userId) {

        //将想要的广告信息放入bannerListCallable
        Callable<List> bannerListCallable = () -> adService.queryIndex();
        Callable<List> channelListCallable = () -> categoryService.queryChannel();


        Callable<List> couponListCallable = () -> couponService.queryNewUserList(0, 3, "add_time", "desc");

        Callable<List> newGoodsListCallable = () -> productService.queryNewPro(0, 6, "add_time", "desc");

        Callable<List> hotGoodsListCallable = () -> productService.queryHotPro(0, 6, "add_time", "desc");

        Callable<List> brandListCallable = () -> brandService.queryList(0, 4, "add_time", "desc");

        Callable<List> topicListCallable = () -> topicService.query(0, 4, "add_time", "desc");

        Callable<List> floorGoodsListCallable = this::queryCategoryList;

        //给此接口设置执行任务
        FutureTask<List> bannerFutureTask = new FutureTask<>(bannerListCallable);
        FutureTask<List> channelFutureTask = new FutureTask<>(channelListCallable);

        FutureTask<List> couponFutureTask = new FutureTask<>(couponListCallable);

        FutureTask<List> newGoodsFutureTask = new FutureTask<>(newGoodsListCallable);

        FutureTask<List> hotGoodsFutureTask = new FutureTask<>(hotGoodsListCallable);

        FutureTask<List> brandListFutureTask = new FutureTask<>(brandListCallable);

        FutureTask<List> topicListFutureTask = new FutureTask<>(topicListCallable);

        FutureTask<List> floorGoodsListTask = new FutureTask<>(floorGoodsListCallable);

        //通过多线程池执行器执行所设置的任务
        executorService.submit(bannerFutureTask);
        executorService.submit(channelFutureTask);
        executorService.submit(couponFutureTask);
        executorService.submit(newGoodsFutureTask);
        executorService.submit(hotGoodsFutureTask);
        executorService.submit(brandListFutureTask);
        executorService.submit(topicListFutureTask);
        executorService.submit(floorGoodsListTask);
        HashMap<String, Object> map = new HashMap<>();
        //将数据返回给前端
        try {
            map.put("banner", bannerFutureTask.get());
            map.put("channel", channelFutureTask.get());
            map.put("couponList", couponFutureTask.get());
            map.put("newGoodsList", newGoodsFutureTask.get());
            map.put("hotGoodsList", hotGoodsFutureTask.get());
            map.put("brandList", brandListFutureTask.get());
            map.put("topicList", topicListFutureTask.get());
            map.put("floorGoodsList", floorGoodsListTask.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseUtil.ok(map);
    }

    private List<Map> queryCategoryList() {
        ArrayList<Map> categoryList = new ArrayList<>();
        //获取1级无推荐类目
        List<Category> catL1List = categoryService.queryL1Selective(0, 4);
        for (Category category : catL1List) {
            //找到1级类目下的二级类目
            List<Category> catL2List = categoryService.queryByPid(category.getId());
            ArrayList<Integer> L2List = new ArrayList<>();
            for (Category catL2 : catL2List) {
                //获取2级类目的id
                L2List.add(catL2.getId());
            }


            List<Product> productList;
            if (L2List.size() == 0) {
                productList = new ArrayList<>();
            } else {
                //通过二级类目id查询商品
                productList = productService.queryByCategory(L2List, 0, 4, "add_time", "desc");
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", category.getId());
            map.put("name", category.getName());
            map.put("goodsList", productList);
            categoryList.add(map);

        }

        return categoryList;
    }


}


